package com.jgame.secondarymarket.domain.product.repository.db;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jgame.secondarymarket.domain.product.entity.Specification;
import com.jgame.secondarymarket.domain.product.repository.SpecificationRepository;
import com.jgame.secondarymarket.domain.product.repository.db.mapper.SpecificationMapper;
import com.jgame.secondarymarket.infrastructure.excetpions.BussnessException;
import com.jgame.secondarymarket.interfaces.query.page.product.PageSpecificationQuery;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @title: SpecificationRepositoryImpl
 * @Author Wangsj
 * @Date: 2021/7/21
 */
@Service
public class SpecificationRepositoryImpl extends ServiceImpl<SpecificationMapper, Specification> implements SpecificationRepository {
    @Override
    public List<Specification> listByProductId(Long productId) {
        return this.list(new LambdaQueryWrapper<Specification>()
                .eq(Specification::getProductId, productId));
    }

    @Override
    public Optional<Specification> findByValue(Long productId, String value) {
        return Optional.ofNullable(
                this.getOne(new LambdaQueryWrapper<Specification>()
                        .eq(Specification::getProductId, productId)
                        .eq(Specification::getValue, value))
        );
    }

    @Override
    public Specification findByValueOrThrow(Long productId, String value) {
        return this.findByValue(productId, value)
                .orElseThrow(() -> BussnessException.throwException("该规格不存在"));
    }

    @Override
    public List<Specification> findByValues(Long productId, List<String> values) {
        return this.list(new LambdaQueryWrapper<Specification>()
                .eq(Specification::getProductId, productId)
                .in(Specification::getValue, values));
    }

    @Override
    public Optional<Specification> findById(Long productId, Long specificationId) {
        return Optional.ofNullable(
                this.getOne(new LambdaQueryWrapper<Specification>()
                        .eq(Specification::getProductId, productId)
                        .eq(Specification::getId, specificationId))
        );
    }

    @Override
    public Specification findByIdOrThrow(Long productId, Long specificationId) {
        return this.findById(productId, specificationId)
                .orElseThrow(() -> BussnessException.throwException("该规格不存在"));
    }

    @Override
    public void delByProductId(Long productId) {
        this.remove(new LambdaQueryWrapper<Specification>()
                .eq(Specification::getProductId, productId));
    }

    @Override
    public void delByIds(Long productId, List<Long> specificationIds) {
        this.remove(new LambdaQueryWrapper<Specification>()
                .eq(Specification::getProductId, productId)
                .and(p -> p.in(Specification::getId, specificationIds)));
    }

    @Override
    public IPage<Specification> getPage(PageSpecificationQuery query) {
        Page<Specification> sPage = new Page<>(query.getCurrent(), query.getSize());
        LambdaQueryWrapper<Specification> queryWrapper = new QueryWrapper<Specification>().lambda();
        if (null != query.getProductId()) {
            queryWrapper.eq(Specification::getProductId, query.getProductId());
        }

        queryWrapper.orderByDesc(Specification::getUpdateTime);
        return page(sPage, queryWrapper);
    }

    @Override
    public List<Specification> listByProductIds(List<Long> productIds) {
        return this.list(new LambdaQueryWrapper<Specification>().
                in(Specification::getProductId, productIds));
    }


    /**
     * 查询商品属性相关的说有规格
     *
     * @param productId
     * @param propertiesIds
     * @return
     */
    @Override
    public List<Specification> listByProductIdAndPropertiesIds(Long productId, List<Long> propertiesIds) {
        //拷贝propertiesId，后续需要对这个list进行操作
        List<Long> pids = new ArrayList<>();
        CollectionUtil.addAll(pids, new Object[propertiesIds.size()]);
        Collections.copy(pids, propertiesIds);

        if (CollectionUtil.isEmpty(pids)) {
            throw BussnessException.throwException("propertiesIds不能为空");
        }
        //除第一个参数为and外其他为or，实现查询所有商品属性id关联的商品规格
        LambdaQueryWrapper<Specification> lamdba = new LambdaQueryWrapper<Specification>().eq(Specification::getProductId, productId)
                .and(qw -> qw.like(Specification::getValue, pids.get(0)));
        pids.remove(0);
        pids.stream().forEach(p -> {
            lamdba.or().like(Specification::getValue, p);
        });
        return this.list(lamdba);
    }
}


